//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    page: 0,
    showLoading: true,
    noMoretip: false,
  },
  onLoad: function (e) {
    var that = this;
    if (e && e.enterfrom) {
      var enterfrom = e.enterfrom == 'share' ? true : false;
      that.setData({
        enterfrom: enterfrom
      })
    }
  },
  onShow: function () {
    var that = this;
   
    if (!app.globalData.plumSession) {
      app.plumSessionCallback = plumSession => {
        that.requestIndex();
        that.requestGrouplist();
        app.requestAd(that);
      }
    } else {
      that.requestIndex();
      that.requestGrouplist();
      app.requestAd(that);
    }
  },
  requestIndex: function () {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_mall_group_index'
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          if (res.data.data.template.title) {
            wx.setNavigationBarTitle({
              title: res.data.data.template.title
            });
          }
          var categoryList = app.splitArrData(res.data.data.shortcut, 8);
          that.setData({
            indexInfo: res.data.data,
            slideImgUrls: res.data.data.slide,
            shortMenu: res.data.data.shortcut,
            categoryList: categoryList,
            title: res.data.data.template.title,
            coupon: res.data.data.coupon
          })
        } else {
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  requestGrouplist: function () {
    var that = this;
    var page = that.data.page;
    //发起请求，获取列表列表
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true,
      duration: 10000
    });
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_mall_group_list',
        suid: app.globalData.suid,
        type: 0,
        page: page,
        version: 1
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          // var shopname = res.data.data.name;
          var allArr = [];
          var initArr = that.data.shopGoods;
          var curArr = res.data.data.goods;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data.goods;
          }
          that.setData({
            shopGoods: allArr
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          // console.log(that.data);
          // wx.setStorage({
          //   key: "goodslistInfor",
          //   data: that.data.shopGoods,
          //   success: function (e) {
          //     console.log("存储成功")
          //   }
          // })
        } else {
          console.log(res.data)
          if (page <= 0) {
            that.setData({
              shopGoods: [],
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    that.requestIndex();
    that.requestGrouplist();
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestGrouplist();
    }
  },
  goodDetail: function (e) {
    var goodid = e.currentTarget.dataset.goodid;
    var status = e.currentTarget.dataset.status;
    // if(status==3){
    //   return;
    // }
    wx.navigateTo({
      url: '/pages/groupGoodDetail/groupGoodDetail?goodid=' + goodid
    })
  },
  searchPage: function () {
    wx.navigateTo({
      url: '/pages/searchList/searchList'
    })
  },
  groupGoods: function (e) {
    var that = this;
    var menuId = e.currentTarget.dataset.id;
    var menuname = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/subpages0/groupGoodsList/groupGoodsList?menuid=' + menuId + '&menuname=' + menuname
    })
  },
  onShareAppMessage: function (e) {
    var that = this;
    if (e.target) {
      var index = e.target.dataset.index;
      var coupon = this.data.coupon;
      coupon[index].needShare = 0;
      this.setData({
        coupon: coupon
      })
    }
    var title = this.data.title;
    var shareInfo = app.globalData.shareInfo;
    title = shareInfo.shareTitle ? shareInfo.shareTitle : title;
    var cover = shareInfo.shareCover ? shareInfo.shareCover : '';
    app.getPoint(that);
    return {
      title: title,
      imageUrl: cover,
      path: '/pages/groupIndexPage/groupIndexPage?enterfrom=share'
    }
  },
  toCuslink: function (e) {
    var link = e.currentTarget.dataset.link;
    console.log(link);
    wx.navigateTo({
      url: link
    })
  },
  getCoupon: function (e) {
    var that = this;
    var curId = e.currentTarget.dataset.id;
    that.setData({
      curId: curId
    })
    app.getSubId(that, 'applet_coupon_receive', 'toGetCoupon');
  },
  // 领取优惠券跳转
  toGetCoupon: function (e) {
    var that = this;
    var id = that.data.curId;
    console.log(id);
    wx.showLoading({
      title: '领取中',
      mask: true
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_coupon_receive',
        cid: id
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          app.errorTip(that, res.data.data.msg, 2000);
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },
})